import {
  existplayer,
  Get_xiuwei,
  sortBy,
  sleep,
  ForwardMsg,
  isNotNull,
  Read_player,
  Read_najie,
  __PATH,
  get_ranking_money_img,
  get_ranking_power_img,
  data
} from '../../model/index.js'
import { AppName } from '../../../config.js'
import { plugin } from '../../../import.js'
import { readdirSync } from 'fs'
export class TopList extends plugin {
  constructor() {
    super({
      name: 'Yunzai_Bot_TopList',
      dsc: '修仙模块',
      event: 'message',
      priority: 600,
      rule: [
        {
          reg: /^(#|\/)天榜$/,
          fnc: 'TOP_xiuwei'
        },
        {
          reg: /^(#|\/)灵榜$/,
          fnc: 'TOP_lingshi'
        },
        {
          reg: /^(#|\/)封神榜$/,
          fnc: 'TOP_Immortal'
        },
        {
          reg: /^(#|\/)至尊榜$/,
          fnc: 'TOP_genius'
        }
      ]
    })
  }

  //封神榜
  async TOP_Immortal(e) {
    let user_id = e.user_id
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false
    let msg = ['___[封神榜]___']
    let playerList = []
    //数组
    let temp = []
    let files = readdirSync(
      './plugins/' + AppName + '/resources/data/xiuxian_player'
    ).filter((file) => file.endsWith('.json'))
    for (let file of files) {
      file = file.replace('.json', '')
      playerList.push(file)
    }
    let i = 0
    for (let player_id of playerList) {
      //(攻击+防御*0.8+生命*0.5)*暴击率=理论战力
      let player = await Read_player(player_id)
      //计算并保存到数组
      let power =
        player.攻击 * 0.9 +
        player.防御 * 1.1 +
        player.血量上限 * 0.6 +
        player.暴击率 * player.攻击 * 0.5
      if (player.level_id < 42) {
        //跳过凡人
        continue
      }
      power = Math.trunc(power)
      temp[i] = {
        power: power,
        qq: player_id,
        name: player.name,
        level_id: player.level_id
      }
      i++
    }
    //根据力量排序
    temp.sort(sortBy('power'))
    console.log(temp)
    let length
    if (temp.length > 10) {
      //只要十个
      length = 10
    } else {
      length = temp.length
    }
    let j
    for (j = 0; j < length; j++) {
      msg.push(
        '第' +
          (j + 1) +
          '名' +
          '\n道号：' +
          temp[j].name +
          '\n战力：' +
          temp[j].power +
          '\nQQ:' +
          temp[j].qq
      )
    }
    await ForwardMsg(e, msg)
    return false
  }

  //#至尊榜
  async TOP_genius(e) {
    let user_id = e.user_id
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false
    let msg = ['___[至尊榜]___']
    let playerList = []
    //数组
    let temp = []
    let files = readdirSync(
      './plugins/' + AppName + '/resources/data/xiuxian_player'
    ).filter((file) => file.endsWith('.json'))
    for (let file of files) {
      file = file.replace('.json', '')
      playerList.push(file)
    }
    let i = 0
    for (let player_id of playerList) {
      //(攻击+防御+生命*0.5)*暴击率=理论战力
      let player = await Read_player(player_id)
      //计算并保存到数组
      let power =
        (player.攻击 + player.防御 * 0.8 + player.血量上限 * 0.6) *
        (player.暴击率 + 1)
      if (player.level_id >= 42) {
        //跳过仙人the记录
        continue
      }
      power = Math.trunc(power)
      temp[i] = {
        power: power,
        qq: player_id,
        name: player.name,
        level_id: player.level_id
      }
      i++
    }
    //根据力量排序
    temp.sort(sortBy('power'))
    console.log(temp)
    let length
    if (temp.length > 10) {
      //只要十个
      length = 10
    } else {
      length = temp.length
    }
    let j
    for (j = 0; j < length; j++) {
      msg.push(
        '第' +
          (j + 1) +
          '名' +
          '\n道号：' +
          temp[j].name +
          '\n战力：' +
          temp[j].power +
          '\nQQ:' +
          temp[j].qq
      )
    }
    await ForwardMsg(e, msg)
    return false
  }

  async TOP_xiuwei(e) {
    let user_id = e.user_id
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false

    let usr_paiming
    let File = readdirSync(__PATH.player_path)
    File = File.filter((file) => file.endsWith('.json'))
    let File_length = File.length
    let temp = []
    for (let i = 0; i < File_length; i++) {
      let this_qq = File[i].replace('.json', '')
      this_qq = this_qq
      let player = await Read_player(this_qq)
      let sum_exp = await Get_xiuwei(this_qq)
      if (!isNotNull(player.level_id)) {
        e.reply('请先#同步信息')
        return false
      }
      //境界名字需要查找境界名
      let level = data
        .Level_list()
        .find((item) => item.level_id == player.level_id).level
      temp[i] = {
        总now_exp: sum_exp,
        境界: level,
        name: player.name,
        qq: this_qq
      }
    }
    //排序
    temp.sort(sortBy('总now_exp'))
    usr_paiming = temp.findIndex((temp) => temp.qq === user_id) + 1
    let Data = []
    if (File_length > 10) {
      File_length = 10
    } //最多显示前十
    for (let i = 0; i < File_length; i++) {
      temp[i].名次 = i + 1
      Data[i] = temp[i]
    }
    let thisplayer = await data.getData('player', user_id)
    let img = await get_ranking_power_img(e, Data, usr_paiming, thisplayer)
    e.reply(img)
    return false
  }

  //TOP_lingshi
  async TOP_lingshi(e) {
    let user_id = e.user_id
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false
    let usr_paiming
    let File = readdirSync(__PATH.player_path)
    File = File.filter((file) => file.endsWith('.json'))
    let File_length = File.length
    let temp = []
    for (let i = 0; i < File_length; i++) {
      let this_qq = File[i].replace('.json', '')
      this_qq = this_qq
      let player = await Read_player(this_qq)
      let najie = await Read_najie(this_qq)
      let lingshi = player.money + najie.money
      temp[i] = {
        ls1: najie.money,
        ls2: player.money,
        money: lingshi,
        name: player.name,
        qq: this_qq
      }
    }
    //排序
    temp.sort(sortBy('money'))
    let Data = []
    usr_paiming = temp.findIndex((temp) => temp.qq === user_id) + 1
    if (File_length > 10) {
      File_length = 10
    } //最多显示前十
    for (let i = 0; i < File_length; i++) {
      temp[i].名次 = i + 1
      Data[i] = temp[i]
    }
    await sleep(500)
    let thisplayer = await data.getData('player', user_id)
    let thisnajie = await data.getData('najie', user_id)
    let img = await get_ranking_money_img(
      e,
      Data,
      usr_paiming,
      thisplayer,
      thisnajie
    )
    e.reply(img)
    return false
  }
}
